local hx__moyin = fk.CreateSkill{
  name = "hx__moyin",
  tags = { Skill.Permanent , Skill.Compulsory },
}


hx__moyin:addEffect(fk.TurnStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    local mark = player:getTableMark("@&hx__moyin_emoji")
    for i = 1, 8 do
      if not table.contains(mark, "hxtx__chaijunemoji" .. i) then
        return player:hasSkill(hx__moyin.name)
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local emojis = {}
    local mark = player:getTableMark("@&hx__moyin_emoji")
    for i = 1, 8 do
      if not table.contains(mark, "hxtx__chaijunemoji" .. i) then
        table.insert(emojis, "hxtx__chaijunemoji" .. i)
      end
    end
    local toget = table.random(emojis)
    room:addTableMark(player, "@&hx__moyin_emoji", toget)
    player.general = toget
    player.deputyGeneral = ""
    room:broadcastProperty(player, "general")
    room:broadcastProperty(player, "deputyGeneral")
  end,
})

hx__moyin:addEffect(fk.AfterCardsMove, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    local mark = player:getTableMark("@&hx__moyin_emoji")
    for i = 1, 8 do
      if not table.contains(mark, "hxtx__chaijunemoji" .. i) then
        if player:hasSkill(hx__moyin.name) and Fk:currentRoom():getCurrent() ~= nil and player:usedEffectTimes(self.name) < 2 then
          for _, move in ipairs(data) do
            if move.from == player then
              for _, info in ipairs(move.moveInfo) do
                if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                  return true
                end
              end
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local emojis = {}
    local mark = player:getTableMark("@&hx__moyin_emoji")
    for i = 1, 8 do
      if not table.contains(mark, "hxtx__chaijunemoji" .. i) then
        table.insert(emojis, "hxtx__chaijunemoji" .. i)
      end
    end
    local toget = table.random(emojis)
    room:addTableMark(player, "@&hx__moyin_emoji", toget)
    player.general = toget
    player.deputyGeneral = ""
    room:broadcastProperty(player, "general")
    room:broadcastProperty(player, "deputyGeneral")
  end,
})

hx__moyin:addEffect(fk.BeforeHpChanged, {
  can_refresh = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__moyin.name) and Fk:currentRoom():getCurrent() ~= nil and data.num ~= 0 then
      return true
    end
  end,
  on_refresh = function(self, event, target, player, data)
    for i = 1, math.abs(data.num) do
      if player:getMark("@hx__moyin_hp-turn") < #player:getTableMark("@&hx__moyin_emoji") then
        player.room:addPlayerMark(player, "@hx__moyin_hp-turn", 1)
      end
    end
  end,
})

hx__moyin:addEffect(fk.BeforeHpChanged, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__moyin.name) and Fk:currentRoom():getCurrent() ~= nil then
      if #target:getTableMark("@&hx__moyin_emoji") > 0 and (data.num < 0 or data.reason == "loseHp") then
        local e = player.room.logic:getEventsOfScope(GameEvent.ChangeHp, 1, function (e)
          return e.data.who == target and Fk:currentRoom():getCurrent() ~= nil and e.data ~= data and (e.data.num < 0 or e.data.reason == "loseHp")
        end, Player.HistoryTurn)
        if #e == 0 then
          event:setCostData(self, { n = 4 })
          return true
        end
      elseif target == player and data.num ~= 0 then
        local num = 0
        local cannum
        player.room.logic:getEventsOfScope(GameEvent.ChangeHp, 999, function (e)
          if e.data.who == player and e.data.num ~= 0 and Fk:currentRoom():getCurrent() ~= nil and e.data ~= data then
            num = num + math.abs(e.data.num)
          end
        end, Player.HistoryTurn)
        if #player:getTableMark("@&hx__moyin_emoji") > num then
          cannum = #player:getTableMark("@&hx__moyin_emoji") - num
          local tochange = math.abs(data.num) - cannum
          if tochange > 0 then
            event:setCostData(self, { n = 2 , num = tochange })
            return true
          end
        else
          event:setCostData(self, { n = 1 })
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = event:getCostData(self).n
    if n == 1 then
      data.prevented = true
    elseif n == 2 then
      local num = event:getCostData(self).num
      if num > 0 then
        data.num = data.num - num
      else
        data.num = num - data.num
      end
    elseif n == 4 then
      local can = table.filter(player.room.alive_players, function (p)
        return #p:getTableMark("@&hx__moyin_emoji") > 0
      end)
      local to = table.random(can)
      data.who = to
      if to ~= target then
        player.room:sendLog{
          type = "%from的体力值减少因【模因】将改为%to的体力值减少！",
          from = target.id,
          to = {to.id},
        }
      end
    end
  end,
})


return hx__moyin
